// pages/doctor-programme/doctor-programme.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  // 本地图片
    useId:'',
    type:1,
    mold:true,
    timeSortbox: false,
    status: false,
    typeShow:false,
    statusShow:false,
    states: ["1", "2", "3"],
    gender:'male',
    operationType:[],   // 手术类型
    num:'',
    pageNum:1,
    pageSize :5,
    oneTypeId:'',
    doctorState: 1,           // 查询状态(1 待提供 2 已提供 3 已采纳 )
    timeSort:1,              // 1 正序  2 倒序
    statusArr1: [{ name: "待提供", id: "1" }, { name: "已结束", id: ["2", "3"] }],
    statusArr2: [{ name: "已提供", id: "1" }, { name: "已结束", id: ["2", "3"] }],
    oneTypeId:'',
    listData:[],        // 数据列表
    doctorRankId:'',    // 医生级别Id
    orderList: [],
    typeName:'手术类型',
    stateName:'状态筛选'
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      useId:wx.getStorageSync('userid')
    })
    app.showLoading();
   
  },

  onShow:function(){
    this.getDemandList();
  },

  // 浏览
  getViewNum(e){
    let exampleId = e.currentTarget.dataset.exampleId;
  },

  // 点赞
  getPoint(e) {
    
    let schemeid = e.currentTarget.dataset.schemeid;
    app.request({
      url: 'doctor/demandApi/updatePoint',
      data: {
        'schemeId': schemeid
      },
      success: (res) => {
        this.data.listData.forEach(item => {
          if (item.schemeId == schemeid) {
            item.isPoint = 1
            item.pointNum += 1
          }
          this.setData({
            listData: this.data.listData
          })
        })
      },
      fail(rel) {
        console.log(rel)
      }
    })
    
  },

  // 评论
  toTalk(e){
    let schemeid = e.currentTarget.dataset.schemeid;
    wx.navigateTo({
      url: '/pages/doctor-programme-assess/doctor-programme-assess?schemeid=' + schemeid
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    app.showLoading();
    this.setData({
      pageSize: this.data.pageSize + 5
    })
    if (this.data.listData.length == this.data.num){
      wx.showToast({ 
        title: '没有更多了哦~',
        icon: 'none',
        duration: 1000
      });
    }else{
      this.getDemandList();
    }
    
  },

  // 获取需求列表
  getDemandList(){
    const that = this;
    const { pageNum, pageSize, doctorState, timeSort, useId, oneTypeId, states } = this.data;
    const param = {
      oneTypeId,
      pageNum,
      pageSize,
      doctorState,
      timeSort,
      states,
      systemType :2,
      useId
    };
    app.request({
      url: "doctor/demandApi/getDemandList",
      data: param,
      success: function (res) {
        wx.hideLoading()
        that.setData({
          num: res.page.total,
          listData:res.data
        })
      },
      fail: function (res) {
        // app.requestFail(res);
      }
    });
  },
  // 手术状态
  switch(e){
    const type = e.currentTarget.dataset.type;
    const doctorState = e.currentTarget.dataset.states;

    this.setData({
      type,
      doctorState,
      typeShow: false,
      statusShow: false,
      stateName: '状态筛选',
      typeName: '手术类型',
      timeSort: 1, 
      timeSortbox: false,
    })
    this.getDemandList();
  },
  // 筛选
  doFilter(e){
    const index = e.currentTarget.dataset.index;
   
    if( index==1 ){
      this.setData({
        mold:true,
        timeSortbox: false,
        status: false,
        statusShow: false
      })   
      this.oneTypeServiceList(); 
    }
    if (index == 2) {
      this.setData({
        mold: false,
        timeSortbox: !this.data.timeSortbox,
        status: false,
       
        typeShow: false,
        statusShow: false,
      })
      if (this.data.timeSort==1){
        this.setData({
          timeSort:2
        })
      }else{
        this.setData({
          timeSort: 1
        })
      }
      this.getDemandList(); 
    }
    if (index == 3) {
      this.setData({
        mold: false,
        timeSortbox: false,
        status: true,
        typeShow: false,
        statusShow: !this.data.statusShow
      })
    }
  },

  // 一级服务产品类别列表
  oneTypeServiceList: function (e) {
    const that = this;
    app.request({
      url: 'shopp/goodsTypeApi/getPage',
      data: {
        'type': 2,
      },
      success: function (res) {
       that.setData({
         operationType: res.data.slice(1),
         typeShow: !that.data.typeShow
       })
      }
    })
  },

  // 手术类型筛选
  operationTypeScreen(e){
  
    const oneTypeId = e.currentTarget.dataset.id;
    const typeName = e.currentTarget.dataset.name;
    this.setData({
      typeShow:false,
      oneTypeId,
      typeName
    })
    this.getDemandList();
  },

  // 状态筛选
  screenStatus(e){
    const states = e.currentTarget.dataset.id;
    const stateName = e.currentTarget.dataset.name;
    this.setData({
      statusShow: false,
      states,
      stateName
    })
    this.getDemandList();
  },

  entry(e){
    const cityRankId= e.currentTarget.dataset.cityrankid;
   
    if(e.currentTarget.dataset.state ==="已结束"){
      app.showModal("该方案已逾期结束");
      return;
    }
    if(this.data.type == 1){
      wx.navigateTo({
        url: '/pages/doctor-programme-condition/doctor-programme-condition?doctorRankId =' + e.currentTarget.dataset.doctorrankid + "&demandId=" + e.currentTarget.dataset.demandid + '&cityRankId=' + cityRankId
      });
    }else{
      wx.navigateTo({
        url: '/pages/doctor-programmeDetail/doctor-programmeDetail?demandId=' + e.currentTarget.dataset.demandid
      });
    }
  },
  // 预览大图
  previewImage(e) {
    let urls = [];
    const imgArr = this.data.listData;
    const imgUrl = this.data.imgUrl;
    const current = e.currentTarget.dataset.src;
    const id = e.currentTarget.dataset.demandid;

    for (let i = 0; i < imgArr.length; i++) {
      if (imgArr[i].demandId == id){
        imgArr[i].bigImgArray.forEach(item => {
          urls.push(imgUrl + item)
        })
       }
    }
  
   
    wx.previewImage({
      current: urls[current],
      urls
    })
  },

  // 预览评论大图
  preImage(e){
    let urls = [];
    const imgArr = this.data.listData;
    const imgUrl = this.data.imgUrl;
    const current = e.currentTarget.dataset.src;
    const id = e.currentTarget.dataset.demandid;

    for (let i = 0; i < imgArr.length; i++) {
      if (imgArr[i].demandId == id) {
        imgArr[i].comment.bigImages.forEach(item => {
          urls.push(imgUrl + item)
        })
      }
    }

   
    wx.previewImage({
      current: urls[current],
      urls
    })
  }

})